package com.miracle9.game.dao;
import com.miracle9.game.entity.UserMail;
import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

@Repository
public class UserMailServiceDao extends SqlOperating<UserMail, Integer>
{
  @Cacheable(value={"springCache"}, key="'getUserMailList'+#userId")
  public List<UserMail> getByUserIdOrderByDatetime(Integer userId)
  {
    return q_CreateSqlList("from UserMail where userId = ? order by datetime desc", new Object[] { userId });
  }
  
  @CacheEvict(value={"springCache"}, key="'getUserMailList'+#userMail.userId")
  public Integer saveReturnInteger(UserMail userMail)
  {
    return (Integer)saveReturn(userMail);
  }
  
  @CacheEvict(value={"springCache"}, key="'getUserMailList'+#userId")
  public void deleteByIds(String ids, Integer userId)
  {
    String[] idArr = ids.split(",");
    for (int i = 0; i < idArr.length; i++) {
    	deleteById(Integer.valueOf(Integer.parseInt(idArr[i])));
    }
  }
  
  @CacheEvict(value={"springCache"}, key="'getUserMailList'+#userId")
  public void updateStatus_1ById(Integer id, Integer userId)
  {
	  q_CreateSql("update UserMail set status=1 where id=?", new Object[] { id }).executeUpdate();
  }
  
  public List<UserMail> getByMailId(int mailId)
  {
    return q_CreateSql("from UserMail where mailId = ?", new Object[] { Integer.valueOf(mailId) }).list();
  }
  
  public void deletePastMail(String date)
  {
	  q_CreateSql("delete from UserMail where datetime<?", new Object[] { date }).executeUpdate();
	  q_CreateSql("delete from Mail where datetime<?", new Object[] { date }).executeUpdate();
  }
}
